import '../../../public.dart';

class AmountInputWidget extends StatelessWidget {
  final TextEditingController controller;
  final UserModel model;

  const AmountInputWidget({
    super.key,
    required this.controller,
    required this.model,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
      ),
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            'Withdraw Amount',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.w600,
              color: Colors.grey[700],
            ),
          ),
          const SizedBox(height: 16),
          _buildAmountInputField(),
          const SizedBox(height: 8),
          // 显示当前余额
          Text(
            'Tips: Minimum withdraw amount is ₦1000',
            style: TextStyle(fontSize: 12, color: Colors.grey[600]),
          ),

          Center(
            child: GestureDetector(
              onTap: () {
                controller.text = model.availableBalance.toString();
              },
              child: Container(
                padding: const EdgeInsets.symmetric(vertical: 8),
                child: Text(
                  'Extract all',
                  style: TextStyle(
                    fontSize: 14,
                    color: const Color(0xFF1DE9B6),
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildAmountInputField() {
    return TextFormField(
      controller: controller,
      keyboardType: TextInputType.number,
      style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w600),
      decoration: InputDecoration(
        hintText: '0.00',
        hintStyle: TextStyle(color: Colors.grey[400], fontSize: 20),
        contentPadding: const EdgeInsets.symmetric(
          horizontal: 16,
          vertical: 20,
        ),
        errorStyle: TextStyle(
          color: Colors.red.shade600,
          fontSize: 12,
          fontWeight: FontWeight.w500,
        ),
        prefixIcon: Container(
          alignment: Alignment.center,
          width: 40,
          child: Text(
            '₦',
            style: const TextStyle(fontSize: 26, color: Color(0xFF1081EB)),
          ),
        ),
        // 底部边框样式
        enabledBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: Colors.grey.shade300, width: 1),
        ),
        focusedBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: Color(0xFF1081EB), width: 2),
        ),
        errorBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: Colors.red.shade600, width: 1),
        ),
        focusedErrorBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: Colors.red.shade600, width: 2),
        ),
      ),
      validator: (value) {
        final balance = model.availableBalance;
        if (value == null || value.trim().isEmpty) {
          return 'Please enter withdrawal amount';
        }

        final amount = double.tryParse(value);
        if (amount == null) {
          return 'Please enter valid amount';
        }

        if (amount <= 0) {
          return 'Amount must be greater than 0';
        }

        if (amount > balance) {
          return 'Insufficient balance';
        }

        if (amount < 1000) {
          return 'Minimum withdrawal is ₦1000';
        }

        return null;
      },
    );
  }
}
